using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DALDeliverable
/// </summary>
[System.ComponentModel.DataObject]
public class DALDeliverable
{
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
    public static List<Deliverable> GetDeliverables()
    {
        return DAL.GetDataItems<Deliverable>(SQL.GetDeliverables, new string[] { }, new object[] { });
    }

    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
    public static List<Deliverable> GetDeliverablesByDeliverableID(int deliverableID)
    {
        return DAL.GetDataItems<Deliverable>(SQL.GetDeliverablesByDeliverableID, new string[] { "@DeliverableID" }, new object[] { deliverableID });
    }

    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
    public static List<Deliverable> GetDeliverablesByCustomerID(int customerID)
    {
        return DAL.GetDataItems<Deliverable>(SQL.GetDeliverablesByCustomerID, new string[] { "@CustomerID" }, new object[] { customerID });
    }

    public static int InsertDeliverable(int customerID, string type, int serviceID, float weight, int distanceID, string receiverName, string receiverAddress, int receiverCityID)
    {
        SqlCommand command = DAL.CreateCommand();
        command.CommandText = SQL.InsertDeliverable;
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@CustomerID", customerID);
        command.Parameters.AddWithValue("@Type", type);
        command.Parameters.AddWithValue("@ServiceID", serviceID);
        command.Parameters.AddWithValue("@Weight", weight);
        command.Parameters.AddWithValue("@DistanceID", distanceID);
        command.Parameters.AddWithValue("@ReceiverName", receiverName);
        command.Parameters.AddWithValue("@ReceiverAddress", receiverAddress);
        command.Parameters.AddWithValue("@ReceiverCityID", receiverCityID);
        if (command.Connection.State == ConnectionState.Closed)
            command.Connection.Open();
        int n = Int32.Parse(command.ExecuteScalar().ToString());
        command.Connection.Close();
        return n;
    }

    public static int DeleteDeliverables(ArrayList deliverablesIDs)
    {
        SqlCommand command = DAL.CreateCommand();
        command.CommandType = CommandType.Text;

        string s = "";
        foreach (string id in deliverablesIDs)
        {
            s += id + ",";
        }
        s = s.Substring(0, s.LastIndexOf(","));
        command.CommandText = SQL.DeleteDeliverables + "(" + s + ")";

        return DAL.ExecuteNonQuery(command);
    }

    public static int UpdateDeliverable(int id, int customerID, string type, int serviceID, float weight, int distanceID, bool status, string receiverName, string receiverAddress, int receiverCityID)
    {
        SqlCommand command = DAL.CreateCommand();
        command.CommandText = SQL.UpdateDeliverable;
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@DeliverableID", id);
        command.Parameters.AddWithValue("@CustomerID", customerID);
        command.Parameters.AddWithValue("@Type", type);
        command.Parameters.AddWithValue("@ServiceID", serviceID);
        command.Parameters.AddWithValue("@Weight", weight);
        command.Parameters.AddWithValue("@DistanceID", distanceID);
        command.Parameters.AddWithValue("@Status", status);
        command.Parameters.AddWithValue("@ReceiverName", receiverName);
        command.Parameters.AddWithValue("@ReceiverAddress", receiverAddress);
        command.Parameters.AddWithValue("@ReceiverCityID", receiverCityID);
        return DAL.ExecuteNonQuery(command);
    }
}
